

#import "UIViewController+FMCommon.h"

#import <objc/runtime.h>

#import "AppDelegate.h"

@implementation UIViewController (FMCommon)

+(void)load
{
    Swizzle([UIViewController class], @selector(init), @selector(customInit));
}

- (void)customInit
{
    if ([NSStringFromClass([self class]) rangeOfString:@"RootController"].location == NSNotFound ) {
        self.hidesBottomBarWhenPushed = YES;
    }
    [self customInit];
}


void Swizzle(Class c, SEL origSEL, SEL newSEL)
{
    Method origMethod = class_getInstanceMethod(c, origSEL);
    Method newMethod = nil;
    if (!origMethod) {
        origMethod = class_getClassMethod(c, origSEL);
        if (!origMethod) {
            return;
        }
        newMethod = class_getClassMethod(c, newSEL);
        if (!newMethod) {
            return;
        }
    }else{
        newMethod = class_getInstanceMethod(c, newSEL);
        if (!newMethod) {
            return;
        }
    }
    if(class_addMethod(c, origSEL, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))){
        class_replaceMethod(c, newSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    }else{
        method_exchangeImplementations(origMethod, newMethod);
    }
}


+ (UIViewController*)findLastViewController:(UIViewController*)vc
{
    if (vc.presentedViewController) {
        return [UIViewController findLastViewController:vc.presentedViewController];
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
        UISplitViewController* svc = (UISplitViewController*) vc;
        if (svc.viewControllers.count > 0) {
            return [UIViewController findLastViewController:svc.viewControllers.lastObject];
        } else {
            return vc;
        }
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController* svc = (UINavigationController*) vc;
        if (svc.viewControllers.count > 0) {
            return [UIViewController findLastViewController:svc.topViewController];
        } else {
            return vc;
        }
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        UITabBarController* svc = (UITabBarController*) vc;
        if (svc.viewControllers.count > 0) {
            return [UIViewController findLastViewController:svc.selectedViewController];
        } else {
            return vc;
        }
    } else {
        return vc;
    }
}

+ (UIWindow*)nowWindow
{
    UIWindow * win = nil;
    if (@available(iOS 13.0, *)) {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
            if (windowScene.activationState == UISceneActivationStateForegroundActive){
                for (UIWindow *window in windowScene.windows){
                    if (window.isKeyWindow){
                        win = window;
                        break;
                    }
                }
            }
        }
        return win;
    }else {
        return  UIApplication.sharedApplication.keyWindow;
    }
}

+ (UIViewController*)tabBarViewController
{
    UIViewController *viewController = nil;
    if (@available(iOS 13.0, *)) {
        viewController = [UIViewController nowWindow].rootViewController;
        if (viewController == nil) {
            return nil;
        }
    }else {
        viewController = [UIViewController nowWindow].rootViewController;
        if (viewController == nil) {
            return nil;
        }
    }
    if ([viewController isKindOfClass:UITabBarController.class]) {
        return viewController;
    }
    return nil;
}

+ (UIViewController *)currentViewController
{
    UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [UIViewController findLastViewController:viewController];
}

+ (UINavigationController*)findNavigationController
{
    return [UIViewController currentViewController].navigationController;
}

@end
